package com.hnucm.cooklife.ui.fragment;

import static com.blankj.utilcode.util.ColorUtils.getColor;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.ColorUtils;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.hnucm.cooklife.R;
import com.hnucm.cooklife.base.BaseFragment;
import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.ui.activity.ChatActivity;
import com.hnucm.cooklife.ui.activity.LoginActivity;
import com.hnucm.cooklife.ui.activity.MomentListActivity;
import com.hnucm.cooklife.ui.activity.SearchWordActivity;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CollectFragment extends BaseFragment {

    @BindView(R.id.collect_tab)
    QMUITabSegment mSlidingTabLayout;

    @BindView(R.id.collect_viewpager)
    ViewPager mViewPager;

    @BindView(R.id.search_box)
    TextView mTextView;

    @BindView(R.id.collect_top)
    View mView;


    private int mCurTab;

    private Context mContext;

    private List<String> mTabNames = new ArrayList<>();

    private ArrayList<Fragment> mFragmentArrayList = new ArrayList<>();


    public static CollectFragment getInstance(){
        CollectFragment collectFragment = new CollectFragment();
        return collectFragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.collect_fragment;
    }

    @Override
    protected void init() {
        mContext = getActivity().getApplicationContext();
        initStatusBar();
        setChildViewVisibility(View.VISIBLE);
        mCurTab = 0;
        initFragment();
        intiViewPager();
        initSearch();
        initTop();
    }
    private void initTop() {
        ImageButton m1 = (ImageButton) mView.findViewById(R.id.search_expanded_back_button);
        m1.setImageResource(R.drawable.ic_post);
        ImageButton m2 = (ImageButton) mView.findViewById(R.id.search_expanded_magnifying_glass);
        m2.setImageResource(R.drawable.ic_edit);

    }

    private void initSearch() {
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(getActivity(), SearchWordActivity.class);
//                startActivity(intent);
            }
        });
    }


    private void initStatusBar() {
        getActivity().getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
        if (ColorUtils.calculateLuminance(getColor(R.color.white)) >= 0.5) {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    private void setChildViewVisibility(int visibility) {
        mSlidingTabLayout.setVisibility(visibility);

        mViewPager.setVisibility(visibility);
    }

    private void initFragment() {
        mTabNames.add(0,"收藏");
        mTabNames.add(1,"浏览历史");
        mFragmentArrayList.add(0,new CoDishFragment());
        mFragmentArrayList.add(1,new CoHisFragment());
    }

    private void intiViewPager() {
        mViewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragmentArrayList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentArrayList == null ? 0: mFragmentArrayList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return mTabNames.get(position);
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurTab = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mSlidingTabLayout.setupWithViewPager(mViewPager);
    }


    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
